package leetcode;

public class CountAndSay {

	public static void main(String[] args) {
		CountAndSay c = new CountAndSay();
		System.out.println(c.countAndSay(2));
		System.out.println(c.countAndSay(3));
		System.out.println(c.countAndSay(4));
		System.out.println(c.countAndSay(5));
		System.out.println(c.countAndSay(6));
	}
	public String countAndSay(int n) {
//		if (n == 1) {
//			return "1";
//		}
		return help(n, "1"); // first is 1
	}
	public String help(int n, String string){
		if(n == 1){
			return string;
		}
		char[] chars = string.toCharArray();
		StringBuilder builder = new StringBuilder();
		int count = 1;
		for(int i = 0; i < chars.length; i++){
			while(i < chars.length - 1 && chars[i] == chars[i + 1]){
				i++;
				count++;
			}
			builder.append(count).append(chars[i]);
			count = 1;
		}
		string = help(n - 1, builder.toString());
		return string;
	}
}
